package com.kintreda.ieco_server.util.json;

import java.util.List;
import java.util.Map;

import com.kintreda.ieco_server.bean.account.Menu;
import com.kintreda.ieco_server.util.Assert;

public class CombotreeUtil {

	public Combotree toString(List<Menu> children, Combotree tree) {
		
		Menu menu = null;
		for (int i = 0; i < children.size(); i++) {
			menu = children.get(i);

			if (tree.getId().equals(menu.getPid())) {
				Combotree subTree = new Combotree();
				subTree.setId(menu.getId());
				subTree.setText(menu.getName());
				tree.getChildren().add(subTree);

				toString(children, subTree);
			}
		}
		return tree;
	}

	public static Menu menuJSON(List<Object> menuList, Menu root) {
		Menu menu = null;
		for (int i = 0; i < menuList.size(); i++) {
			menu = (Menu)menuList.get(i);
			if (menu.getPid().equals(root.getId())) {
				root.getChildren().add(menu);
				menuJSON(menuList, menu);
			}
		}
		return root;
	}
	
	
	public static Tree treeJSON(List<Map<String, Object>> treeList, Tree root) {
		
		for (int i = 0; i < treeList.size(); i++) {
			Tree tree = new Tree();
			tree.setId(treeList.get(i).get("id").toString());
			tree.setPid(treeList.get(i).get("pid").toString());
			tree.setName(treeList.get(i).get("name").toString());
			tree.setNameEn(Assert.notNull(treeList.get(i).get("nameEn")) ? treeList.get(i).get("nameEn").toString() : "");
			tree.setAbbreviation(Assert.notNull(treeList.get(i).get("abbreviation")) ? treeList.get(i).get("abbreviation").toString() : "");
			if(treeList.get(i).get("pid").toString().equals("0")||treeList.get(i).get("pid").toString()=="0"){
				tree.setState("closed");
			}
			if (tree.getPid().equals(root.getId())) {
				root.getChildren().add(tree);
				treeJSON(treeList, tree);
			}
		}
		
		return root;
	}
}